package com.jlt.web.account;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.security.springsecurity.SpringSecurityUtils;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.jlt.entity.account.Post;
import com.jlt.exception.ServiceException;
import com.jlt.service.account.PostManager;
import com.jlt.web.CrudActionSupport;
import com.jlt.web.util.Struts2ActionUtils;

/**
 * 用户管理Action.
 * 
 * 使用Struts2 convention-plugin annotation定义Action参数. 演示带分页的管理界面.
 * 
 * @author calvin
 */
// 定义URL映射对应/account/post.action
@Namespace("/account")
// 定义名为reload的result重定向到post.action, 其他result则按照convention默认.
@Results( { @Result(name = CrudActionSupport.RELOAD, location = "post.action", type = "redirect") })
public class PostAction extends CrudActionSupport<Post> {

	private static final long serialVersionUID = 1L;
	private static final String ENTITY_NAME = "职位";
	@Autowired
	private PostManager postManager;

	// -- 页面属性 --//
	private Long id;
	private Post entity;
	private List<Post> allPostList;
	private Page<Post> page = new Page<Post>(15);// 每页15条记录

	// -- ModelDriven 与 Preparable函数 --//
	public void setId(Long id) {
		this.id = id;
	}

	public Post getModel() {
		return entity;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (id != null) {
			entity = postManager.getPost(id);
		} else {
			entity = new Post();
			entity.setCreatedBy(SpringSecurityUtils.getCurrentUserName());
			entity.setCreatedTime(new Date());
			entity.setUpdatedBy(SpringSecurityUtils.getCurrentUserName());
			entity.setUpdatedTime(new Date());
		}
	}

	// -- CRUD Action 函数 --//
	@Override
	public String list() throws Exception {
		List<PropertyFilter> filters = PropertyFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		page = postManager.searchPost(page, filters);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		// checkedRoleIds = entity.getRoleIds();
		return INPUT;
	}
	
	public String chooseList() throws Exception {
		allPostList = postManager.getAllPost();
		return "chooseList";
	}
	
	@Override
	public String save() throws Exception {
		entity.setUpdatedBy(SpringSecurityUtils.getCurrentUserName());
		entity.setUpdatedTime();
		postManager.savePost(entity);
		message = Struts2ActionUtils.getDwzOperateMessage(Struts2ActionUtils.SAVE,
				Struts2ActionUtils.SUCCESSFUL, ENTITY_NAME,Struts2ActionUtils.CLOSE_CURRENT);
		Struts2Utils.renderJson(message);
		return null;
	}

	@Override
	public String delete() throws Exception {
		Map<String, String> message=new HashMap<String, String>();
		try {
			postManager.deletePost(id);
			message = Struts2ActionUtils.getDwzOperateMessage(Struts2ActionUtils.DELETE,
					Struts2ActionUtils.SUCCESSFUL, ENTITY_NAME);
		} catch (ServiceException e) {
			logger.error(e.getMessage(), e);
			message = Struts2ActionUtils.getDwzOperateMessage(Struts2ActionUtils.DELETE,
					Struts2ActionUtils.FAILURE, ENTITY_NAME);
		}
		Struts2Utils.renderJson(message);
		return null;
	}

	// -- 页面属性访问函数 --//
	/**
	 * list页面显示用户分页列表.
	 */
	public Page<Post> getPage() {
		return page;
	}

	public List<Post> getAllPostList() {
		return allPostList;
	}

	public void setAllPostList(List<Post> allPostList) {
		this.allPostList = allPostList;
	}

}
